package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApUnLikesBehaviorService;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class ApUnLikesBehaviorServiceImpl implements ApUnLikesBehaviorService {
    @Autowired
    private CacheService cacheService;
    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //通过线程常量池获得当前app用户
        ApUser apUser = AppThreadLocalUtil.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);

        }

       if(dto.getType()==0){
           //不喜欢该文章:把文章id和用户id和对应的数据存到redis中
           cacheService.hPut("unlike_"+dto.getArticleId().toString(),apUser.getId().toString(), JSON.toJSONString(dto));
       }else{
           cacheService.hDelete("unlike_"+dto.getArticleId().toString(),apUser.getId().toString());
       }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
